public class MyBlockQueue {
    private int[] item = new int[1000];
    private int head = 0;
    //指向队列头部
    private int tail = 0;
    //指向队列尾部
    private volatile int size = 0;
    //此时阻塞队列中的元素

    //入队列方法
    public synchronized void put(int val) {
        if (size >= item.length) {
            //此时队列满,此时再加入队列要进行阻塞
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        item[tail] = val;
        tail++;
        if (tail >= item.length) {
            tail = 0;
        }
        size++;
        //唤醒出队列的wait
        this.notify();
    }

    //出队列
    public synchronized int take() {
        if (size == 0) {
            //此时队列为空,再次出队列要进行阻塞,知道有元素加入
            try {
                this.wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        int i = item[head];
        head++;
        if (head >= item.length) {
            head = 0;
        }
        size--;
        //唤醒入队列的wait();
        this.notify();
        return i;
    }
}
